package com.zhg.javakc.modules.group.tree.controller;

import com.zhg.javakc.base.util.CommonUtil;
import com.zhg.javakc.modules.group.tree.entity.GroupEntity;
import com.zhg.javakc.modules.group.tree.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author an
 * @version v0.0.1
 * @date 2019/11/25 16:12
 */

@Controller
@RequestMapping("/group")
public class GroupController {

    @Autowired
    GroupService groupService;

    @RequestMapping("queryAllOrg")
    @ResponseBody
    public List<Map<String,Object>> queryAllOrg()
    {

        List<Map<String,Object>> list = groupService.queryAllOrg();
        return list;
    }

    @RequestMapping("create")
    public String insert(GroupEntity groupEntity)
    {
        //生成组织表主键id
        groupEntity.setSupplierGroupId(CommonUtil.uuid());
        groupService.save(groupEntity);
        return "group/list";
    }

    @RequestMapping("delete")
    public String delete(String groupId)
    {
        //得到当前groupId的所有子节点的数据
        List<Map<String,Object>> list = groupService.queryByOrgId(groupId);

        if(null != list && list.size()>0){
            for(Map<String,Object> map : list){
                delete(map.get("id").toString());
            }
            groupService.deleteOrg(groupId);
        }else {
            //##删除当前节点
            groupService.deleteOrg(groupId);
        }

        return "group/list";
    }

}
